<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    body {
      background-color: #4646c0;
      height: 100vh;
      display: flex;
      justify-content: center;
      align-items: center;
    }

    img {
      width: 100%;
      height: 100%;
    }

    .con {
      display: flex;
      gap: 10px;
    }

    .con .item {
      width: 150px;
      height: 150px;
      border: 3px solid #333;
      background-color: #ccc;
      border-radius: 8px;
    }

    .item.drag-over {
      border: 3px dashed #ccc;
      background-color: #333;
    }
  </style>
</head>

<body>
  <div class="con">
    <div class="item">
      <img src="https://picsum.photos/360/460?random=1" draggable="true" alt="" />
    </div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
  </div>
</body>
<script>
  const item = document.querySelectorAll('.item');
  const img = document.querySelector('img');
  img.addEventListener('dragstart', () => {
    setTimeout(() => {
      img.style.visibility = 'hidden';
    }, 0);
  });
  img.addEventListener('dragend', () => {
    img.style.visibility = 'visible';

  });
  item.forEach((item) => {
    // 拖拽到进入目标元素触发
    item.addEventListener('dragenter', (e) => {
      // console.log("enter");
      item.classList.add('drag-over');
    });
    // 离开目标元素触发
    item.addEventListener('dragleave', (e) => {
      // console.log("dragleave");
      item.classList.remove('drag-over');
    });
    //拖拽过程中持续触发
    item.addEventListener('drag', (e) => {
      // console.log('drag');
    });
    // 拖拽触碰到目标元素时触发（激活目标元素的hover）
    item.addEventListener('dragover', (e) => {
      e.preventDefault();
      console.log('dragover');
      item.appendChild(img)
    });
  });
</script>

</html>